/* 
 * Copyright (C) 2012 UTSChedule
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package utsc.apps.utschedule.ui.activities.adapters;

import java.util.ArrayList;
import java.util.List;

import utsc.apps.utschedule.R;
import utsc.apps.utschedule.classes.CourseMark;
import utsc.apps.utschedule.classes.UofTGrade;
import android.content.Context;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * This is a list adapter for {@link  utsc.apps.utschedule.ui.activities.CalculatorPredictListFragment CalculatorPredictListFragment}.
 *
 */
public class CalculatorPredictListAdapter extends ArrayAdapter<CourseMark> {

	private static final String ADD_TEXT="+ Add new course mark";
	private ArrayList<CourseMark> _courseMarks;

	public CalculatorPredictListAdapter(Context context, int textViewResourceId,
			List<CourseMark> objects) {
		super(context, textViewResourceId, objects);
		_courseMarks=new ArrayList<CourseMark>(objects);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView==null) {
			convertView=LayoutInflater.from(getContext()).inflate(R.layout.course_prediction_list_item, null);
			holder=new ViewHolder();
			holder._txtCourseCode=(TextView) convertView.findViewById(R.id.txt_list_course);
			holder._txtCourseMark=(TextView) convertView.findViewById(R.id.txt_list_course_mark);
			holder._markLayout=(LinearLayout) convertView.findViewById(R.id.mark_layout);
			holder._grade=new UofTGrade(0,100,0);
			
			convertView.setTag(holder);
		}else {
			holder=(ViewHolder) convertView.getTag();
		}
		CourseMark courseMark=getItem(position);
		if (courseMark==null) {
			holder._txtCourseCode.setText(ADD_TEXT);
			holder._txtCourseCode.setTypeface(null,Typeface.BOLD);
			holder._txtCourseCode.setGravity(Gravity.CENTER);
			holder._markLayout.setVisibility(View.GONE);
		}else {
			holder._grade.set_marks(courseMark.get_mark());
			holder._grade.calculatePercentage();
			holder._txtCourseCode.setTypeface(null,Typeface.NORMAL);
			holder._txtCourseCode.setGravity(Gravity.NO_GRAVITY);
			holder._txtCourseCode.setText(courseMark.toString());
			holder._markLayout.setVisibility(View.VISIBLE);
			holder._txtCourseMark.setText(holder._grade.toMarkString());
		}
		
		return convertView;
		
	}
	
	public void setItem(CourseMark courseMark, int position) {
		if (position>=_courseMarks.size()) {
			_courseMarks.add(courseMark);
		}
		else {
			_courseMarks.set(position, courseMark);
		}
		notifyDataSetChanged();
	}
	
	
	private static class ViewHolder{
		TextView _txtCourseCode;
		TextView _txtCourseMark;
		LinearLayout _markLayout;
		UofTGrade _grade;
	}
	
	/**
	 * Returns the total count of the items in the adapter. The count is always
	 * n+1 because we need at least one row to hold the action button.
	 */
	@Override
	public int getCount() {
		if (_courseMarks!=null && !_courseMarks.isEmpty()) {
			return _courseMarks.size()+1;
		}
		return 1;
	}
	
	public void set_courseMarks(ArrayList<CourseMark> _courseMarks) {
		this._courseMarks = _courseMarks;
	}
	
	public ArrayList<CourseMark> get_courseMarks() {
		return _courseMarks;
	}
	
	@Override
	public CourseMark getItem(int position) {
		if (position<_courseMarks.size()) {
			return _courseMarks.get(position);
		}
		return null;
	}
	
}
